package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Attention;
import cn.wolfcode.trip.base.domain.Great;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.AttentionMapper;
import cn.wolfcode.trip.base.mapper.GreatMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.service.IAttentionService;
import cn.wolfcode.trip.base.service.IGreatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AttentionServiceImpl implements IAttentionService {
   @Autowired
    private AttentionMapper attentionMapper;

   @Autowired
   private UserMapper userMapper;
    @Override
    public  Attention get(Long userId, Long userGetId) {
        return attentionMapper.selectByUserIdAndUserGetId(userId,userGetId);
    }

    @Override
    public void deleteGreat(Long userId, Long userGetId) {
        attentionMapper.deleteAttention(userId,userGetId);
    }

    @Override
    public void insertGreat(Attention attention) {
        //关注的时间
        attention.setGreatTime(new Date());
        attentionMapper.insert(attention);
    }

    @Override
    public void subGreatNum(Long userId) {
        //查询出来该文章的点赞数,将点赞数量减一
        int num= attentionMapper.selectGreatNum(userId);
        //更新文章的点赞数量
        attentionMapper.updateGreatNum(userId,num-1);
    }
    @Override
    public void addGreatNum(Long userId) {
        int num= attentionMapper.selectGreatNum(userId);
        //更新用户的收藏总数数量
         int i =num+1;
        attentionMapper.updateGreatNum(userId,i);
    }

    @Override
    public List<User> getUg_Id(Long id) {
        List<Attention> ug_id = attentionMapper.getUg_Id(id);
        List<User> attern = new ArrayList<>();
        for (Attention attention : ug_id) {
            User userGetId = attention.getUserId();
            User user = userMapper.selectByPrimaryKey(userGetId.getId());
            attern.add(user);
        }
        return attern;
    }
}
